import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
import { get, post } from '@/utils/request'
import { useAuthStore, useSettingStore } from '@/store'

export function fetchChatAPI<T = any>(
  prompt: string,
  options?: { conversationId?: string; parentMessageId?: string },
  signal?: GenericAbortSignal,
) {
  return post<T>({
    url: '/chat',
    data: { prompt, options },
    signal,
  })
}

export function fetchChatConfig<T = any>() {
  return post<T>({
    url: '/config',
  })
}

export function fetchChatAPIProcess<T = any>(
  params: {
    prompt: string
    options?: { conversationId?: string; parentMessageId?: string }
    signal?: GenericAbortSignal
    onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void },
) {
  const settingStore = useSettingStore()
  const authStore = useAuthStore()

  let data: Record<string, any> = {
    prompt: params.prompt,
    options: params.options,
  }

  if (authStore.isChatGPTAPI) {
    data = {
      ...data,
      systemMessage: settingStore.systemMessage,
      temperature: settingStore.temperature,
      top_p: settingStore.top_p,
    }
  }

  return post<T>({
    url: '/chat-process',
    data,
    signal: params.signal,
    onDownloadProgress: params.onDownloadProgress,
  })
}

export function fetchSession<T>() {
  return post<T>({
    url: '/session',
  })
}

export function fetchVerify<T>(token: string) {
  return post<T>({
    url: '/verify',
    data: { token },
  })
}

// export function post_gxy_Chat(
//   query: string,
//   conversation_id = '1',
//   history_len = -1,
//   history: string[] = [],
//   stream = false,
//   l_name = 'gxy',
//   temperature = 0.7,
//   max_tokens = 0,
//   prompt_name = 'default',
// ) {
//   return post({
//     url: 'http://127.0.0.1:8088/chat',
//     data: {
//       query,
//       conversation_id,
//       history_len,
//       history,
//       stream,
//       modelName,
//       temperature,
//       max_tokens,
//       prompt_name,
//     },
//   })
// }
const baseURL = import.meta.env.VITE_APP_API_BASE_URL
export function postChat(
  // signal: GenericAbortSignal,
  query: string,
  modelName: string,
  conversationId = '1',
  historyLen = -1,
  history: string[] = [],
  stream = false,
  temperature = 0.7,
  maxTokens = 0,
  promptName = 'default',
) {
  console.log(modelName)
  return post({
    url: baseURL + '/chat',
    // signal,
    data: {
      query,
      conversationId,
      historyLen,
      history,
      stream,
      modelName,
      temperature,
      maxTokens,
      promptName,
    },
  })
}

interface Document {
  docTitle: string
  docContent: string
}

export function generateReport(
  docList: Document[],
  userTitle: string,
  userOutline: string[],
  userKeywords: string,
) {
  return post({
    url: 'http://127.0.0.1:8088/generateReport',
    // signal,
    data: {
      docList,
      userTitle,
      userOutline,
      userKeywords,
    },
  })
}

export function inserttopic(
  topic: string,
  user_id: number,
) {
  return post({
    url: 'http://127.0.0.1:8088/add_topic',
    data: {
      topic,
      user_id,
    },
  })
}

export function postInsertHistory(
  userid: number,
  role: string,
  content: string,
  topic: string,
  time: Date,
) {
  return post({
    url: 'http://127.0.0.1:8088/insert_history',
    data: {
      userid,
      role,
      content,
      topic,
      time,
    },
  })
}

export function getByTopic(
  topic: string,
) {
  return get({
    url: 'http://127.0.0.1:8088/by_topic',
    data: {
      topic,
    },
  })
}

export function getAllTopic(

) {
  return get({
    url: 'http://127.0.0.1:8088/all_topic',
    data: {
    },
  })
}
